using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Minio;
using Minio.DataModel.Tags;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

namespace Kevin.Dapr.Api.Controllers
{
    public partial class MinIOController : IDynamicApiController
    {
        [HttpPost]
        public async Task Upload(IFormFile file)
        {
            var bucketName = "adimage";
            var bucketArgs = new BucketExistsArgs().WithBucket(bucketName);
            bool found = await _client.BucketExistsAsync(bucketArgs);
            //如果桶不存在则创建桶
            if (!found)
            {
                var makeBucketArgs = new MakeBucketArgs().WithBucket(bucketName);
                await _client.MakeBucketAsync(makeBucketArgs);
            }

            var stream = file.OpenReadStream();

            var putObjectArgs = new PutObjectArgs()
                .WithBucket(bucketName)
                //.WithFileName(file.FileName)
                .WithContentType(file.ContentType)
                .WithObjectSize(file.Length)
                .WithObject("10086")
                .WithHeaders(new Dictionary<string, string> { { "filename", file.FileName } })
                .WithTagging(Tagging.GetObjectTags(new Dictionary<string, string> { { "filename", file.FileName } }))
                .WithStreamData(stream);

            await _client.PutObjectAsync(putObjectArgs);
        }

        public async Task<IActionResult> Download(string id) 
        {
            var bucketName = "adimage";
            MemoryStream stream = new MemoryStream();
            var args = new GetObjectArgs()
                .WithBucket(bucketName)
                .WithObject(id)
                .WithCallbackStream(cb => {
                    cb.CopyTo(stream);
                });
            var data = await _client.GetObjectAsync(args);
            stream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(stream, "application/octet-stream") { FileDownloadName = data.MetaData["filename"].ToString() };
            
        }

        public async Task Copy()
        {
            var bucketName = "adimage";
            var cpSrcArgs = new CopySourceObjectArgs()
                .WithBucket(bucketName)
                .WithObject("DB_DBHMall-20221110.7z");
            var args = new CopyObjectArgs()
                .WithBucket(bucketName)
                .WithObject(Guid.NewGuid().ToString() + ".7z")
                .WithCopyObjectSource(cpSrcArgs);

            await _client.CopyObjectAsync(args);
        }

        public async Task thumbnail(int width, int height)
        {
            string filePath = $"{_hostingEnvironment.ContentRootPath}\\test.png";
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            byte[] buffur = new byte[fs.Length];
            fs.Read(buffur, 0, (int)fs.Length);
            

            var image = Image.Load(buffur);
            image.Mutate(x => x.Resize(width, height));
            var memeryStream = new MemoryStream();
            await image.SaveAsync($"{_hostingEnvironment.ContentRootPath}\\test2.jpg");
        }
    }

    public partial class MinIOController
    {
        private readonly MinioClient _client;
        private readonly IWebHostEnvironment _hostingEnvironment;

        public MinIOController(MinioClient client, IWebHostEnvironment hostingEnvironment)
        {
            _client = client;
            _hostingEnvironment = hostingEnvironment;
        }
    }
}
